'''
处理电容相关的值
'''

import re
from excutor.excutor import Excutor

class Capacity(Excutor):

    def __init__(self, data):
        super().__init__(data)
        self.capacity = '电容'
        self.pattrenNum = re.compile(r"\.?[0-9]+,?\.?[0-9]*")
        self.pattrenUnit = re.compile(r"[pnµum]?F")
    
    @staticmethod
    def Existence(data,capacity = '电容'):
        return data[data['new_key'].str.contains(capacity)]['new_key'].count()
    
    #修改数据表
    def processingData(self):
        indexCapacity = self.data[self.data['new_key'].str.contains(self.capacity)].index
        for index in indexCapacity:
            newValueRef = self._get_new_value(index)
            self.data.loc[index,'new_value_ref'] = newValueRef
        return self.data
    
    #分析并处理old_value,返回new_value_ref
    def _get_new_value(self,index):
        oneRowData = self.data.loc[index]
        old_value = oneRowData['old_value']
        numbers = self.pattrenNum.findall(old_value)
        numbers = [self._compelet_decimal(x) for x in numbers]
        units = self.pattrenUnit.findall(old_value)
        if not (numbers or units):
            return old_value
        elif numbers and units:
            return self._circumatance1(oneRowData,numbers,units)
        elif numbers:
            return self._circumatance2(oneRowData,numbers)
        else:
            return old_value
    
    #返回参考新值的第一种情况：数字和单位都存在
    def _circumatance1(self,oneRowData,numbers,units):
        if len(numbers) == 1:
            return numbers[0] + units[0]
        if len(numbers) >= 2:
            old_value = oneRowData['old_value']
            if re.search('~',old_value):
                return numbers[0] + ' to ' + numbers[-1] + units[0]
            return ','.join([number + units[0] for number in numbers])
        return oneRowData['old_value']

    #返回参考新值的第二种情况：只有数字存在
    def _circumatance2(self,oneRowData,numbers):
        brand = oneRowData['brand']
        new_key = oneRowData['new_key']
        unit = self.pattrenUnit.findall(new_key)

        if len(numbers) == 1:
            if unit:
                return numbers[0] + unit[0]

            if brand == 'ABRACON' or brand == 'BOURNS':
                return numbers[0] + 'pF'

            return numbers[0] + 'pF'

        if len(numbers) >= 2:
            old_value = oneRowData['old_value']
            if unit:
                if re.search('~',old_value):
                    return numbers[0] + ' to ' + numbers[-1] + unit[0]
                return ','.join([number + unit[0] for number in numbers])
            
            if brand == 'ABRACON' or brand == 'BOURNS':
                unit = ['pF']
                if re.search('~',old_value):
                    return numbers[0] + ' to ' + numbers[-1] + unit[0]
                return ','.join([number + unit[0] for number in numbers])

            return ','.join([number + 'pF' for number in numbers])

                

        return oneRowData['old_value']
        
    
    #将简写小数变为正常小数 例如.11;并且去掉逗号
    def _compelet_decimal(self,num):
        if num[0] == '.':
            return '0' + num.replace(',','')
        return num
    
